//
//  LJM_FM_ViewController.m
//  wind
//
//  Created by 李金明 on 16/1/17.
//  Copyright © 2016年 李金明. All rights reserved.
//

#import "LJM_FM_ViewController.h"
#import "LJM_FM_CollectionViewCell.h"
#import "LJM_FM_CollectionReusableView.h"
#import "LJMBase_FM_Model.h"
#import "LJMBase_FM_ONEModel.h"
#import "PCSEQVisualizer.h"
#import "LJMListen_Model.h"

#pragma 音乐播放器界面_SYP
#import "SYPMusicPlay_ViewController.h"


//collectionfowl的坐标
#define COLLECTIONFOWLEDGE 5//边界
#define COLLECTWIDTH 20
#define COLLECTHEADHEIGHT 20//区头的高度
@interface LJM_FM_ViewController () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, retain) UIButton *bactButton;
@property (nonatomic, retain) UIView *barview;
@property (nonatomic, retain) UICollectionView *collectionV;
@property (nonatomic, retain) NSIndexPath *path;//解决重用池问题

#pragma 音乐播放器需要的属性_SYP
@property (nonatomic, retain) NSMutableArray *musicArr;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, assign) NSInteger states;

@end

@implementation LJM_FM_ViewController
- (void)dealloc
{
    self.backDelegate = nil;
    [_musicArr release];
    [_path release];
    [_dataArr release];
    [_collectionV release];
    [_barview release];
    [_bactButton release];
    [super dealloc];
}
- (void)setDataArr:(NSMutableArray *)dataArr
{
    if (_dataArr != dataArr) {
        [_dataArr release];
        _dataArr = [dataArr retain];
    }

    [self.collectionV reloadData];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    //自定义navigationBar
    [self navigationBarView];
    //添加collectionView
    [self addCollectionView];
    
}
#pragma 添加collectionView
- (void)addCollectionView
{
    UICollectionViewFlowLayout *flowLy = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    flowLy.sectionInset = UIEdgeInsetsMake(COLLECTIONFOWLEDGE + COLLECTIONFOWLEDGE, COLLECTIONFOWLEDGE, COLLECTIONFOWLEDGE, COLLECTIONFOWLEDGE);
    flowLy.itemSize = CGSizeMake((self.view.frame.size.width - COLLECTWIDTH) / 2, (self.view.frame.size.width - COLLECTWIDTH) / 4 + COLLECTWIDTH);
    flowLy.minimumLineSpacing = COLLECTIONFOWLEDGE;
    flowLy.minimumInteritemSpacing = COLLECTIONFOWLEDGE;
     flowLy.headerReferenceSize = CGSizeMake(self.view.frame.size.width, COLLECTHEADHEIGHT);
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 100) collectionViewLayout:flowLy];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    self.collectionV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionV];
    [_collectionV release];
    [self.collectionV registerClass:[LJM_FM_CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    [self.collectionV registerClass:[LJM_FM_CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    

}
#pragma collectionView的协议方法
#pragma 个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
 LJMBase_FM_ONEModel *oneModel = [self.dataArr objectAtIndex:section];

    return oneModel.dataArr.count;
}
#pragma cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSInteger index = 100 * indexPath.section + indexPath.row;
    
    // 点击的同一个
//    if (self.index == index) {
//        
//        // 选择状态
//        if (0 == self.states) {
//            self.states = 1;
//            NSLog(@"第一次点击, 开始播放");
//           
//            // 未选择状态
//        } else {
//            self.states = 0;
//            NSLog(@"第二次点击, 开始暂停");
//            
//        }
//        
//        // 点击的不是同一个
//    } else {
//        
//        
//    }
    
    LJM_FM_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
     LJMBase_FM_ONEModel *oneModel = [self.dataArr objectAtIndex:indexPath.section];
    LJMBase_FM_Model *model = [oneModel.dataArr objectAtIndex:indexPath.row];
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.pic_url_240_200] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
    cell.titleLab.text = model.tag_name;
    cell.sonNumLab.text = [NSString stringWithFormat:@"%@首歌", model.quantity];
    if (self.path != indexPath) {
        cell.buttonImageV.image = [UIImage imageNamed:@"play"];
        if (1 == cell.buttonImageV.subviews.count) {
            [cell.buttonImageV.subviews.lastObject removeFromSuperview];
        }
    } else {
        PCSEQVisualizer* eq = [[[PCSEQVisualizer alloc]initWithNumberOfBars:4] autorelease];
        eq.backgroundColor = [UIColor clearColor];
        CGRect frame = eq.frame;
        frame.origin.x = 0;
        frame.origin.y = 0;
        eq.frame = frame;
        cell.buttonImageV.image = [UIImage imageNamed:@"navigationbar"];
        [cell.buttonImageV addSubview:eq];
        
        [eq start];
        
        
        // 记录当前点的第几个
        self.index = index;
        

    }

    return cell;

}
#pragma 组的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.dataArr.count;
}
#pragma 定义区头
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    LJM_FM_CollectionReusableView *headerView=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
    LJMBase_FM_ONEModel *oneModel = [self.dataArr objectAtIndex:indexPath.section];


    headerView.titleLab.text = oneModel.tag_type_name;
    
    return headerView;
    
}
#pragma 点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //对于音乐波浪进行控制
    if (indexPath != self.path && self.path != nil) {
        
        LJM_FM_CollectionViewCell *oneCell = (LJM_FM_CollectionViewCell *)[collectionView cellForItemAtIndexPath:self.path];
        oneCell.buttonImageV.image = [UIImage imageNamed:@"play"];
        [oneCell.buttonImageV.subviews.lastObject removeFromSuperview];
        
        
        LJM_FM_CollectionViewCell *cell = (LJM_FM_CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        
        PCSEQVisualizer* eq = [[[PCSEQVisualizer alloc]initWithNumberOfBars:4] autorelease];
        eq.backgroundColor = [UIColor clearColor];
        CGRect frame = eq.frame;
        frame.origin.x = 0;
        frame.origin.y = 0;
        eq.frame = frame;
        cell.buttonImageV.image = [UIImage imageNamed:@"navigationbar"];
        [cell.buttonImageV addSubview:eq];
        
        [eq start];
        
        self.path = indexPath;
    }
    if (nil == self.path) {
        LJM_FM_CollectionViewCell *cell = (LJM_FM_CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        
        PCSEQVisualizer* eq = [[[PCSEQVisualizer alloc]initWithNumberOfBars:4] autorelease];
        eq.backgroundColor = [UIColor clearColor];
        CGRect frame = eq.frame;
        frame.origin.x = 0;
        frame.origin.y = 0;
        eq.frame = frame;
        cell.buttonImageV.image = [UIImage imageNamed:@"navigationbar"];
        [cell.buttonImageV addSubview:eq];
        
        [eq start];
        
        self.path = indexPath;
    }

    //点击的数据
    LJMBase_FM_ONEModel *oneModel = [self.dataArr objectAtIndex:indexPath.section];
    LJMBase_FM_Model *model = [oneModel.dataArr objectAtIndex:indexPath.row];
    NSString *upStr = @"http://fm.api.ttpod.com/vipradiosong?num=150&tagid=";
    NSString *downStr = @"&app=ttpod&v=v8.1.3.2015112716&user_id=0&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=7.0.4&active=1&net=2&openudid=1389d9dc18cb741587d90a5aead8646421f95dc0&idfa=C9BC818C-A0F5-4F04-8F8D-841910E08DF7&utdid=VmGiKEXHDQ0DAIL2FOnFFzxV&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude= HTTP/1.1";
    NSString *urlStr = [NSString stringWithFormat:@"%@%@%@", upStr, model.tag_id, downStr];
    [LJMNetworkTool getNetWithURL:urlStr body:nil headFile:nil responseStyle:LJMResponseJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *dictionary = responseObject;
        NSMutableArray *array = dictionary[@"data"];
    
        self.musicArr = [LJMListen_Model baseModelWithArr:array];
        
        // 点击播放
        
        [self.backDelegate jumpPageWithArr:self.musicArr indexRow:indexPath.row indexSection:indexPath.section];
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
    
    

    
    
}
#pragma 自定义navigationBar
- (void)navigationBarView
{
   
    self.barview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, NAVIGATIONBARHEIGHT)];
    self.barview.userInteractionEnabled = YES;
    self.barview.backgroundColor = [UIColor colorWithRed:69 / 255.0 green:175 / 255.0 blue:248 / 255.0 alpha:1.0];
    [self.navigationController.navigationBar addSubview:self.barview];
    [_barview release];
    self.bactButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.bactButton setTitle:@"返回" forState:UIControlStateNormal];
    [self.bactButton addTarget:self action:@selector(backButton) forControlEvents:UIControlEventTouchUpInside];
    [self.bactButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.barview addSubview:self.bactButton];
    [self.bactButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.barview.mas_left).offset(10);
        make.width.mas_equalTo(@50);
        make.bottom.equalTo(self.barview.mas_bottom).offset(-5);
        make.height.mas_equalTo(@30);
    }];
    UILabel *titleLab = [[UILabel alloc] init];
    titleLab.text = @"电台";
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.font = [UIFont boldSystemFontOfSize:20];
    titleLab.textColor = [UIColor whiteColor];
    [self.barview addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.barview.mas_centerX);
        make.width.mas_equalTo(@50);
        make.centerY.equalTo(self.bactButton.mas_centerY);
        make.height.mas_equalTo(@30);
    }];

}
#pragma 返回的动作
- (void)backButton
{
    [self.navigationController popViewControllerAnimated:YES];
    [self.barview removeFromSuperview];
    [self.backDelegate backFMAction];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
